package com.web.manager.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.web.manager.pojo.Result;
import com.web.manager.pojo.Student;
import com.web.manager.pojo.User;
import com.web.manager.service.StudentService;
import com.web.manager.service.UserService;
import com.web.manager.exception.ServiceException;
import com.web.manager.utils.TokenUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin  //解决跨域问题
@RestController
@RequestMapping("/student")
public class StudentController {


        @Resource
        private StudentService studentService;

    @Resource
    private UserService userService;





        //新增用户
        @RequestMapping("/add")
        public Result add(@RequestBody Student student){

            try{
                studentService.save(student);  //insertUser//save==>插入一个信息新增
                userService.save(student);

            }catch (Exception e) {
                if (e instanceof DuplicateKeyException) {
                    return Result.error("插入数据库错误,用户名重复");
                } else {
                    return Result.error("系统错误");
                }
            }

            return Result.success();
        }

        //修改
        @PutMapping("/update")
        public Result update(@RequestBody Student student){
            studentService.updateById(student);  //updateUser
            userService.updateById(student);
            return Result.success();
        }


        @DeleteMapping("/delete/{id}")
        public Result delete(@PathVariable Integer id){

            User currentUser = TokenUtils.getCurrentUser();
            if (id.equals(currentUser.getId())) {
                throw new ServiceException("不能删除当前的用户");
            }
            studentService.removeById(id);  //deleteUser
            userService.removeById(id);
            return Result.success();
        }

        @DeleteMapping("/delete/batch")
        public Result batchDelete(@RequestBody List<Integer> ids){

            studentService.removeBatchByIds(ids);   //batchDeleteUser
            userService.removeBatchByIds(ids);
            return Result.success();
        }

        @GetMapping("/selectAll")
        public Result selectAll(){
            List<Student> userList = studentService.list();  //selectAll
            return Result.success(userList);
        }



        //根据id查询
        @GetMapping("/selectById/{id}")
        public Result selectById(@PathVariable Integer id){
            Student student= studentService.getById(id);   //selectById
            return Result.success(student);
        }



        /**
         * 多条件模糊查询用户信息
         * pageNum 当前的页码
         * pageSize 每页查询的个数
         */
        @GetMapping("/selectByPage")
        public Result selectByPage(@RequestParam Integer pageNum,
                                   @RequestParam Integer pageSize,
                                   @RequestParam String username,
                                   @RequestParam String name) {

            QueryWrapper<Student> queryWrapper = new QueryWrapper<Student>();  //条件构造器
            queryWrapper.like(StrUtil.isNotBlank(username),"username",username);
            queryWrapper.like(StrUtil.isNotBlank(name),"name",name);
            //select * from manager_user where username like '%#{username}% and name like '%#{name}%'

            Page<Student> page = studentService.page(new Page<>(pageNum,pageSize), queryWrapper);        //selectByPage
            return Result.success(page);
        }
}


